package bcxd.firstline.credit.machining;

import java.sql.Connection;

import jt.classic.system.context.ISystemContext;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DateTrimmer;
import org.limp.mine.StringTool;

import bcxd.firstline.user.manager.AppUserManager;

/**
 * 信用机构贷款申请审批
 * 
 * @author mido
 * 
 */
public class LoanAuditMachining implements SimpleBeanMachining {

	public SimpleBean machining(SimpleBean bean, Table table,
			CommonBean4HSQ cbh, Connection conn, int type)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (type == -4) {
			if (ISystemContext.invalidSesstion(cbh.getRequest())) {
				throw new MachiningException("invalid session.");
			}

			/* 审核是否通过 */
			String status = StringTool.checkString(bean.getResource().get(
					"LA_AUDITID"), "0");

			IUser user = ISystemContext.getSessionUser(cbh.getRequest());

			// 审核时间
			bean.getResource().put("LA_AUDITTIME", DateTrimmer.getYMDHMS());
			// 审核人ID
			bean.getResource().put("LA_AUDITORID", user.id());
			// 审核人姓名
			bean.getResource().put("LA_AUDITOR", user.iname());
			// 贷款申请审核状态
			bean.getResource()
					.put("LA_STATUS", status.equals("0") ? "-1" : "1");
		} else if (type == 3) {
			/* 审核是否通过 */
			String status = StringTool.checkString(bean.getResource().get(
					"LA_AUDITID"), "0");

			// 贷款请求审核未通过,贷款使用的订单状态恢复为正常,当前订单可以继续为其他贷款申请使用
			if (status.equals("-1")) {
				SimpleBean4Table sbt = new SimpleBean4TableImpl("BCXD_INDENT");
				sbt.getResource().put("idid",
						bean.getResource().get("LA_INDENID"));
				sbt.getResource().put("idapplystatus", "-1");
				BaseworkUtil basework = new BaseworkUtil();
				try {
					basework.manualUpdate1(sbt, conn);
				} catch (Exception ex) {
					throw new MachiningException(
							"release indent for loan-apply error.", ex);
				}
			}
		} else if (type == 4000) {

			/*
			 * IUser user = ISystemContext.getSessionUser(cbh.getRequest()); if
			 * (user.iflag() == 0) { Connection newConn = null; try {
			 * SystemDataSource ds = SystemDataSource.getInstance(); newConn =
			 * ds.getConnection();
			 * 
			 * AppUserManager manager = new AppUserManager(newConn);
			 * 
			 * IUser appUser = manager.getBcxdUserById(user.id());
			 * 
			 * ISystemContext.resetSessionUser(cbh.getRequest(), appUser); }
			 * catch (Exception ex) {
			 * SystemDataSource.rollbackConnection(newConn); throw new
			 * MachiningException("get application-user error.", ex); } finally {
			 * SystemDataSource.closeConnection(newConn); } }
			 */
		}
		return null;
	}

}
